#!/bin/sh
#/****************************************************************************
#**  SIONLIB     http://www.fz-juelich.de/jsc/sionlib                       **
#*****************************************************************************
#**  Copyright (c) 2008-2019                                                **
#**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
#**                                                                         **
#**  See the file COPYRIGHT in the package base directory for details       **
#****************************************************************************/

USAGE1="Usage: sionconfig "
USAGE2="Print compiler and linker flags SIONlib."
USAGE3="  (--com|--ser|--omp|--mpi|--ompi|--gen)   select SIONlib API"
USAGE4="  (--cflags|--libs|--path)                 select output of sionconfig"
USAGE5="  [--32|--64]                              Precision"
USAGE6="  [--be] [--fe] [--mic]                    Cross compile"
USAGE7="  [--gcc]                                  for GCC Compiler"
USAGE8="  [--c|--f77|--f90|--cxx]                  Language selection"
USAGE9="  [-V|--version|-h]                        Version, Help"

if [ $# -eq 0 ]
then
  echo "${USAGE1}"
  echo "${USAGE2}"
  echo "${USAGE3}"
  echo "${USAGE4}"
  echo "${USAGE5}"
  echo "${USAGE6}"
  echo "${USAGE7}"
  echo "${USAGE8}"
  echo "${USAGE9}"
  exit 1
fi

INSTDIR="@INSTDIR@"
ARCH="@ARCH@"
HINTSLIB="@HINTSLIB@"
CACHELIB="@CACHELIB@"
CUDA_LIBRARIES="@CUDA_LIBRARIES@"
CUDA_LIBRARY_PATHS="@CUDA_LIBRARY_PATHS@"
SIONFWD_LIBS="@SIONFWD_LIBS@"
PTHREADSUPPORT="@PTHREADSUPPORT@"

#######################
# Set Defaults
#######################
PREC=""
USESGEN=no
USESMPI=yes
FLIB=""
GCC=""
MPICFLAGS=""
PRECFL=""
DEVICE=""
FE=""
ADDLIBS="${HINTSLIB} ${CACHELIB} ${CUDA_LIBRARY_PATHS} ${CUDA_LIBRARIES} ${SIONFWD_LIBS}"

TASK="dontknow"
USEAPI=serial
USELANG=c
THREADSAFE=no

LIBS="-L${INSTDIR}/lib"
CFLAGS="-I${INSTDIR}/include"
CXXFLAGS="-I${INSTDIR}/include"
FFLAGS="-I${INSTDIR}/include"
MPIFLAGS="-DSION_MPI"
OMPFLAGS="-DSION_OMP"
OMPIFLAGS="-DSION_OMPI"

#######################
# Process command line
#######################
for arg in "$@"
do
  case ${arg} in

  --cflags|-cflags)      TASK=cflags
	                 ;;
  --libs|-libs)          TASK=libs
                         ;;
  --path|-path)          TASK=path
                         ;;

# API
  --com|-com|--common)     USEAPI=common
                              ;;
  --ser|-ser|--sequential) USEAPI=serial
                              ;;
  --mpi|-mpi)                 USEAPI=mpi
                              ;;
  --ompi|-ompi)               USEAPI=ompi
                              ;;
  --omp|-omp)                 USEAPI=omp
                              ;;
  -g|--gen|-gen)              USEAPI=gen
                              ;;
# Threadsafe
  -threadsafe|--threadsafe)   THREADSAFE=yes
                              ;;

# language
  -f77|--f77)            USELANG=f77
                         ;;
  -f90|--f90|--fortran)  USELANG=f90
                         ;;
  -cxx|--cxx)            USELANG=cxx
	                 ;;
  -c|--c)                USELANG=c
                         ;;

  -f|--for|--fortran)    FLIB="@FMPILIB@"
                         ;;

# precision
  -*64)                  PREC=64
	                 PRECFL="_64"
                         ;;
  -*32)                  PREC=32
	                 PRECFL="_32"
	                 ;;
# compiler
  -gcc|--gcc)            USEGCC=yes
	                 GCC="gcc"
                         ;;

# cross compiling
  -fe|--fe)              USEFE=yes
	                 FE="fe"
                         ;;
  -be|--be)              USEBE=yes
	                 FE=""
                         ;;
  --mic)                 DEVICE="mic"
                         ;;

  -v|-V|--version)
	  ${INSTDIR}/bin/sionversion
	  exit 0
          ;;

  -h|--help)
	  ${INSTDIR}/bin/sionversion
	  echo "${USAGE1}"
	  echo "${USAGE2}"
	  echo "${USAGE3}"
	  echo "${USAGE4}"
	  echo "${USAGE5}"
	  echo "${USAGE6}"
	  echo "${USAGE7}"
	  echo "${USAGE8}"
	  echo "${USAGE9}"
  exit 1
                         ;;

  *)                     ;;
  esac
done

#####################
# check architecture
#####################
case ${ARCH} in
    MIC) SIONARCH="-D_SION_MIC"
        GCC=""
        if [ "@${PREC}" = "@" ]
        then
            PREC=64
            PRECFL="_64"
        fi
        ADDLIBS="${ADDLIBS}"
        ;;
    LINUX) SIONARCH="-D_SION_LINUX"
	FE=""
	GCC=""
	if [ "@${PREC}" = "@" ]
	then
	    PREC=64
	    PRECFL="_64"
	fi
        ADDLIBS="${ADDLIBS}"
	;;
    AIXPWR6) SIONARCH="-D_SION_AIX"
	FE=""
	GCC=""
	;;
    DARWIN) SIONARCH="-D_SION_LINUX"
	FE=""
	GCC=""
	;;
    AIX) SIONARCH="-D_SION_AIX"
	FE=""
	GCC=""
	if [ "@${PREC}" = "@" ]
	then
	    PREC=64
	    PRECFL="_64"
	fi
	;;
    BGP) SIONARCH="-D_SION_BGP"
	if [ "@${PREC}" = "@" ]
	then
	    PREC=32
	    PRECFL="_32"
	fi
	;;
    BGQ) SIONARCH="-D_SION_BGQ"
	if [ "@${PREC}" = "@" ]
	then
	    PREC=64
	    PRECFL="_64"
	fi
	;;
    FX) SIONARCH="-D_SION_FX"
	if [ "@${PREC}" = "@" ]
	then
	    PREC=64
	    PRECFL="_64"
	fi
	;;
esac

if [ "@${DEVICE}" = "@mic" ]
then
    if [ "${ARCH}" != "MIC" ]
    then
        echo "ERROR: device ${DEVICE} not supported for SION architecture ${ARCH}!"
        exit 42
    fi
fi

if [ "@${PTHREADSUPPORT}" = "@no" ]
then
    if [ "${THREADSAFE}" = "yes" ]
    then
        echo "ERROR: SIONlib not compiled to be thread-safe!"
        exit 42
    fi
fi


#####################
# OUTPUT
#####################

FFLAGS="${FFLAGS} -I${INSTDIR}/include/mod${FE}${GCC}_${PREC}"
APILANG="${USEAPI}_$USELANG";

if [ "${TASK}" = "path" ]
then
    echo "${INSTDIR}"
fi

if [ "${TASK}" = "cflags" ]
then
    case ${APILANG} in
	common_c)
	    echo "${CFLAGS} ${SIONARCH}"
            ;;
	common_cxx)
	    echo "${CFLAGS} ${SIONARCH}"
            ;;
	common_f77)
	    echo "${FFLAGS}"
            ;;
	common_f90)
	    echo "${FFLAGS}"
            ;;
	serial_c)
	    echo "${CFLAGS} ${SIONARCH}"
            ;;
	serial_cxx)
	    echo "${CFLAGS} ${SIONARCH}"
            ;;
	serial_f77)
	    echo "${FFLAGS}"
            ;;
	serial_f90)
	    echo "${FFLAGS}"
            ;;
	gen_c)
	    echo "${CFLAGS} ${SIONARCH}"
            ;;
	gen_cxx)
	    echo "${CFLAGS} ${SIONARCH}"
            ;;
	gen_f77)
	    echo "${FFLAGS}"
            ;;
	gen_f90)
	    echo "${FFLAGS}"
            ;;
	omp_c)
	    echo "${CFLAGS} ${SIONARCH} ${OMPFLAGS}"
            ;;
	omp_cxx)
	    echo "${CFLAGS} ${SIONARCH} ${OMPFLAGS}"
            ;;
	omp_f77)
	    echo "${FFLAGS}"
            ;;
	omp_f90)
	    echo "${FFLAGS}"
            ;;
	mpi_c)
	    echo "${CFLAGS} ${SIONARCH} ${MPICFLAGS} ${MPIFLAGS}"
            ;;
	mpi_cxx)
	    echo "${CFLAGS} ${SIONARCH} ${MPICFLAGS} ${MPIFLAGS}"
            ;;
	mpi_f77)
	    echo "${FFLAGS}"
            ;;
	mpi_f90)
	    echo "${FFLAGS}"
            ;;
	ompi_c)
	    echo "${CFLAGS} ${SIONARCH} ${MPICFLAGS} ${OMPIFLAGS}"
            ;;
	ompi_cxx)
	    echo "${CFLAGS} ${SIONARCH} ${MPICFLAGS} ${OMPIFLAGS}"
            ;;
	ompi_f77)
	    echo "${FFLAGS}"
            ;;
	ompi_f90)
	    echo "${FFLAGS}"
            ;;
    esac
fi

if [ "${TASK}" = "libs" ]
then
    LANGFL=
    COMMONLIB="-llsioncom${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"

    if [ "${THREADSAFE}" = "yes" ]
    then
        ADDLIBS="${ADDLIBS} ${COMMONLIB}_lock_pthreads"
    else
        ADDLIBS="${ADDLIBS} ${COMMONLIB}_lock_none"
    fi



    SERLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    OMPLIB="-llsionomp${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    MPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    OMPILIB="-llsionompi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    GENLIB="-llsiongen${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"


    LANGFL=_cxx
    CXXSERLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    CXXOMPLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    CXXMPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    CXXOMPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"

    LANGFL=_f77
    F77SERLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    F77OMPLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    F77MPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    F77OMPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"

    LANGFL=_f90
    F90SERLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    F90OMPLIB="-llsionser${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    F90MPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"
    F90OMPILIB="-llsionmpi${FE}${DEVICE}${GCC}${LANGFL}${PRECFL}"

    case ${APILANG} in
	common_c)
	    echo "${LIBS} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	common_cxx)
	    echo "${LIBS} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	common_f77)
	    echo "${LIBS} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	common_f90)
	    echo "${LIBS} ${COMMONLIB}   ${ADDLIBS}"
            ;;

	gen_c)
	    echo "${LIBS} ${GENLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	gen_cxx)
	    echo "${LIBS} ${GENLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	gen_f77)
	    echo "${LIBS} ${GENLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	gen_f90)
	    echo "${LIBS} ${GENLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;

	serial_c)
	    echo "${LIBS} ${SERLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	serial_cxx)
	    echo "${LIBS} ${CXXSERLIB} ${SERLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	serial_f77)
	    echo "${LIBS} ${F77SERLIB} ${SERLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;
	serial_f90)
	    echo "${LIBS} ${F90SERLIB} ${SERLIB} ${COMMONLIB}   ${ADDLIBS}"
            ;;

	omp_c)
	    echo "${LIBS} ${OMPLIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	omp_cxx)
	    echo "${LIBS} ${CXXOMPLIB} ${CXXSERLIB} ${OMPLIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	omp_f77)
	    echo "${LIBS} ${F77OMPLIB} ${F77SERLIB} ${OMPLIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	omp_f90)
	    echo "${LIBS} ${F90OMPLIB} ${F90SERLIB} ${OMPLIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;

	mpi_c)
	    echo "${LIBS} ${MPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	mpi_cxx)
	    echo "${LIBS} ${CXXMPILIB} ${CXXSERLIB} ${MPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	mpi_f77)
	    echo "${LIBS} ${F77MPILIB} ${F77SERLIB} ${MPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	mpi_f90)
	    echo "${LIBS} ${F90MPILIB} ${F90SERLIB} ${MPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;

	ompi_c)
	    echo "${LIBS} ${OMPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	ompi_cxx)
	    echo "${LIBS} ${CXXOMPILIB} ${CXXSERLIB} ${OMPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	ompi_f77)
	    echo "${LIBS} ${F77OMPILIB} ${F77SERLIB} ${OMPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
	ompi_f90)
	    echo "${LIBS} ${F90OMPILIB} ${F90SERLIB} ${OMPILIB} ${GENLIB} ${SERLIB} ${COMMONLIB} ${ADDLIBS}"
            ;;
    esac
fi

exit 0
